WCF Data Services, formerly known as ADO.NET Data
Services, enables the creation and consumption of OData services. OData,
the Open Data Protocol, is a new data-sharing standard that allows for
greater sharing of data between different systems. Before it was called
WCF Data Services, ADO.NET Data Services was one of the very first
Microsoft technologies to support OData with Visual Studio 2008 SP1.
Microsoft has broadened its support of OData in products such as SQL
Server 2008 R2, Windows Azure Storage, and others. This section
discusses how to use WCF Data Services to connect to and query your SQL
Azure database.
1. Creating a Data Service
First you need to create a data service. Follow these steps:
Create
a new ASP.NET web application, and call it WCFDataServiceWebApp. (You
can host data services a number of different environments but this
example uses a web app.)
The
next step in creating a data service on top of a relational database is
to define a model used to drive your data service tier. The best way to
do that is to use the ADO.NET Entity Framework, which allows you to
expose your entity model as a data service. And to do that, you need to
add a new item to your web project. Right-click the web project, and
select New Item. In the Add New Item dialog, select Data from the
Categories list, and then select ADO.NET Entity Data Model from the
Templates list. Give the model the name TechBioModel.edmx, and click OK.
In the first step of the Data Model Wizard, select the Generate From Database option, and click Next.
The
next step is Choose Your Data Connection. Click the New Connection
button, and create a connection to your SQL Azure database. Save the
entity connection settings as TechBioEntities, and then click Next.
The
next step of the wizard is the Choose Your Database Objects page.
Select all the tables. Note the option that is new to ADO.NET Entity
Framework version 4.0, which pluralizes or singularizes generated
objects names. If you leave this option checked, it comes into play
later. Leave it checked, and click Finish.
The
Entity Framework looks at all the tables you selected and creates a
conceptual model on top of the storage schema that you can soon expose
as a data service. In Visual Studio, you should see the Entity Framework
Model Designer with a graphical representation of the tables, called entities, and their relationships. Close the Model Designer—you don't need it for this example.
What
you need to do now is create the data service on top of your data
model. In Solution Explorer right-click the web application and select
Add, then select New Item. In the Add New Item dialog, select the Web
category, then scroll down the list of templates and select the WCF Data
Service template. Enter a name of TechBioDataService the click Add, as
shown in Figure 1
When the ADO.NET Data
Service is added to your project, the associated .cs file will
automatically be displayed in the IDE. As you can see the ADO.NET Data
Service template has generated for you the beginnings of your data
service.
2. Connecting the Service to the Model
Now you need to wire up your
data service to your data model so that the service knows where to get
its data. You know where to do this because as you can see in the code
it tells you where to enter that information. Thus, change the line:
public class TechBioDataService : DataService< /* TODO: put your data source
class name here */ >
To:
public class TechBioDataService : DataService< TechBioEntities >
Wiring up your data service
to the model is as simple as that. Believe it or not, you're ready to
test your service. However, let's finish what you need to do on this
page. By default, the WCF Data Service is secured. The WCF Data Service
needs to be told explicitly which data you want to see. The instructions
in the code tell you this, as you can see in code in the
InitializeService method. Some examples are even provided in the
comments to help you out.
For your example, you don't
want to restrict anything so you really want to unlock all the entities
and explicitly define access rights to the entity sets. You do this by
adding the code below to the InitializeService method. The code below
sets the access rule for the specified entities to All, providing
authorization to read, write, delete, and update data for the specified
entity set:
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("Docs", EntitySetRights.All);
config.SetEntitySetAccessRule("UserDocs", EntitySetRights.All);
config.SetEntitySetAccessRule("Users", EntitySetRights.All);
config.SetEntitySetAccessRule("TechGeoInfo", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
If you don't feel like
specifying each entity one by one, you can optionally specify all the
entities with a single line, as follows:
config.SetEntitySetAccessRule("*", EntitySetRights.All);
The above line assumes that you
want to specify the same rights to all the entities. Not recommended,
but will do for this example. In a production environment you want to
more specific with what rights you specify for each entity.
There are other
EntitySetRights options, such as AllRead, AllWrite, None, ReadSingle,
and WriteAppend. You won't cover them all here but you can read about
them here:
http://msdn.microsoft.com/en-us/library/system.data.services.entitysetrights.aspx
So far, you've created your
Web application, added your data model, and added your WCF Data Service.
Right now your Solution Explorer should look like Figure 2.